<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Manager
 *
 * @author Administrator
 */
class User extends CActiveRecord {

    public $query;
    public $filterLevel;
    public $filterAdmin;
    public $startRegDate;
    public $endRegDate;
    public $startLoginDate;
    public $endLoginDate;

    public function rules() {
        return array(
            array('user_name, user_pass,user_email,user_money', 'required'),
            array('user_name,user_pass,filterAdmin,filterLevel,startRegDate,endRegDate,startLoginDate,endLoginDate', 'length', 'max' => 30, 'min' => 5),
            array('user_name', 'unique'),
            array('user_face', 'file', 'allowEmpty' => true, 'types' => 'gif,jpg,jpeg,png'),
            array('user_email', 'email'),
            array('query', 'length', 'max' => 50),
            array('query,startRegDate,endRegDate,startLoginDate,endLoginDate', 'safe'),
            array('user_create_time,user_login_num,usergroup_level,user_money', 'numerical'),
            array('user_address,user_phone,user_create_ip,user_recommender,user_create_user', 'length', 'max' => 100)
        );
    }

    public function attributeLabels() {
        return array(
            'user_id' => Yii::t('default', '编号'),
            'user_name' => Yii::t('default', '帐户名称'),
            'user_pass' => Yii::t('default', '登录密码'),
            'usergroup_level' => Yii::t('default', '帐户组'),
            'user_face' => Yii::t('default', '用户形象'),
            'user_phone' => Yii::t('default', '联系电话'),
            'user_address' => Yii::t('default', '联系地址'),
            'user_create_time' => Yii::t('default', '创建时间'),
            'user_email' => Yii::t('default', '联系邮箱'),
            'user_create_ip' => Yii::t('default', '创建IP'),
            'user_last_login' => Yii::t('default', '上次登录时间'),
            'user_login_error' => Yii::t('default', '登录失败数'),
            'user_status' => Yii::t('default', '帐号状态'),
            'user_create_user' => Yii::t('default', '创建人'),
            'user_active' => Yii::t('default', '激活状态'),
            'user_gender' => Yii::t('default', '性别'),
            'user_money' => Yii::t('default', '金币'),
            'user_login_num' => Yii::t('default', '登陆次数'),
            'user_add_title' => Yii::t('default', '添加新用户'),
            'level' => Yii::t('default', '用户组'),
            'query' => Yii::t('default', '关键词'),
            'regdate' => Yii::t('default', '注册时间'),
            'loginDate' => Yii::t('default', '登陆时间'),
        );
    }

    /**
     * Returns the static model of the specified AR class.
     * @return CActiveRecord the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return '{{users}}';
    }

    public function getUserStatusArray() {
        return array('yes' => Yii::t("app", "正常"), "no" => Yii::t("app", "关闭"));
    }

    public function getUserActiveArray() {
        return array('yes' => Yii::t("app", "已激活"), "no" => Yii::t("app", "未激活"));
    }

    public function getUserGenderArray() {
        return array('M' => Yii::t("app", "男性"), "F" => Yii::t("app", "女性"), "S" => Yii::t("app", "保密"));
    }

    public function getUserLevel() {
        $usergroup = new UserGroup();
        $data = $usergroup->findAll();
        return CHtml::listData($data, 'usergroup_level', 'usergroup_name');
    }

    public function getUserStatusName($name) {
        $result = $this->getUserStatusArray();
        if (array_key_exists($name, $result)) {
            return $result[$name];
        } else {
            return Yii::t("app", "空");
        }
    }

    /**
     * 组织用户组下拉菜单
     * @return type 
     */
    public function getParentOption() {
        return UserGroup::model()->getParentOption();
    }

    /**
     *
     * @param integer $usergroupId
     * @return String
     */
    public function getUserGroupName($usergroupId) {
        return UserGroup::model()->findByPk($usergroupId)->usergroup_name;
    }

    /**
     * 在数据保存之前执行
     * @return boolean
     */
    public function beforeSave() {
        if (parent::beforeSave()) {
            if ($this->isNewRecord) {
                $this->user_pass = $this->getPass($this->user_pass);
                $this->user_last_login = time();
                $this->user_create_time = time();
                $this->user_login_num = 0;
                $this->user_recommender = "";
                $this->user_create_ip = Yii::app()->request->getUserHost();
                $this->user_create_user = Yii::app()->cookies->admin;
                $this->user_gender = "S";
            }
            return true;
        }
        else
            return false;
    }

    public function getPass($pass) {
        return md5($pass . substr($pass, 0, 3));
    }

    public function filterLevel() {
        $usergroup = UserGroup::model()->findAll(array('order' => 'usergroup_level ASC', 'select' => 'usergroup_level,usergroup_name'));
        $data = array();
        $data[] = Yii::t('default', '全部权限');
        if ($usergroup) {
            foreach ($usergroup as $user) {
                $data[$user['usergroup_level']] = $user['usergroup_name'];
            }
        }
        return $data;
    }

    public function filterAdmin() {
        $usergroup = Manager::model()->findAll(array('select' => 'admin_name'));
        $data = array();
        $data[] = Yii::t('default', '全部管理员');
        if ($usergroup) {
            foreach ($usergroup as $user) {
                $data[$user['admin_name']] = $user['admin_name'];
            }
        }
        return $data;
    }

    public function getLevel() {
        $where = new CDbCriteria();
        $where->select = 'usergroup_name';
        $where->compare('usergroup_level', $this->usergroup_level);
        $usergroup = new UserGroup();
        $data = $usergroup->find($where);
        if($data){
            return $data->usergroup_name;
        }else{
            return null;
        }
    }

    public function getSearch() {
        $criteria = new CDbCriteria();
        if ($this->filterAdmin) {
            $criteria->addCondition('user_create_user = :admin');
            $criteria->params[':admin'] = $this->filterAdmin;
        }
        if ($this->filterLevel) {
            $criteria->addCondition('usergroup_level = :level');
            $criteria->params[':level'] = $this->filterLevel;
        }
        if ($this->query) {
            $criteria->addSearchCondition('user_name', $this->query);
            $criteria->addSearchCondition('user_email', $this->query, false, 'OR');
        }
        if ($this->startRegDate && $this->endRegDate) {
            $stime = strtotime($this->startRegDate);
            $etime = strtotime($this->endRegDate);
            if ($etime > $stime) {
                $criteria->addBetweenCondition('user_create_time', $stime, $etime + 86400);
            }
        }
        if ($this->startLoginDate && $this->endLoginDate) {
            $stime = strtotime($this->startLoginDate);
            $etime = strtotime($this->endLoginDate);
            if ($etime > $stime) {
                $criteria->addBetweenCondition('user_last_login', $stime, $etime + 86400);
            }
        }
        if( $this->user_money > 0){
            $criteria->addCondition(' user_money >= :money');
            $criteria->params[':money'] = (int)$this->user_money;
        }
        return new CActiveDataProvider($this, array(
                    'criteria' => $criteria,
                    'pagination' => array(
                        'pageSize' => 20,
                    ),
                    'sort' => array(
                        'defaultOrder' => 'user_id DESC', //设置默认排序是create_time倒序
                    ),
                ));
    }

    public function relations() {
        return array(
            'usergroup' => array(self::HAS_ONE, 'UserGroup', 'usergroup_level')
        );
    }

}